# echobot.py -- echo messages back to sender

import msnp
import time

class MsnChatListener(msnp.ChatCallbacks):
    def message_received(self, passport_id, display_name, text, charset):
        print '%s: %s' % (passport_id, text)
        self.chat.send_message(text, charset)

class MsnListener(msnp.SessionCallbacks):
    def chat_started(self, chat):
        callbacks = MsnChatListener()
        chat.callbacks = callbacks
        callbacks.chat = chat
        print 'start chat'
    def state_changed(self,state):
        if state == msnp.States.ONLINE:
            print 'you are now online'
    def friend_added(self, list_, passport_id, display_name, group_id = -1):
        if list_==msnp.Lists.REVERSE:
            if self.session.friend_list.privacy_mode==msnp.PrivacyModes.ALLOW and self.session.friend_list.notify_on_add_==False:
                self.session.add_friend(msnp.Lists.ALLOW,passport_id)
    def friend_list_updated (self,friend_list):
        for friend in friend_list.get_friends(msnp.Lists.REVERSE):
            if friend not in friend_list.get_friends(msnp.Lists.ALLOW):
                self.session.add_friend(msnp.Lists.ALLOW,friend.passport_id)
            


msn = msnp.Session(MsnListener())
msn.callbacks.session=msn
msn.notify_on_add(False)
msn.change_privacy_mode(msnp.PrivacyModes.ALLOW)

msn.login('netbigzb@hotmail.com', '19830906')
msn.sync_friend_list()
while True:
    try:
        msn.process(chats = True)
        time.sleep(3)
    except KeyboardInterrupt:
        break
